package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.JsonUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttachmentDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosOperatorDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.AttachmentVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.BcmcPosContext;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.KitePosHandlerContext;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.bss.svcability.util.ListUtil;

/**
 * <Description> <br>
 * 组装报文经办人节点
 * 
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月15日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl <br>
 */
@Service
public class BcmcPosOperatorHandler extends AbstractBcmcPosHandler {

    @Override
    public void doHandle(BcmcPosContext context) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(context.getSceneInstId());
        if (StringUtils.equalsAny(kiteSceneInst.getOperType(), LocalKeyConsts.SCENE_OPER_TYPE_ORDER_SIGN,
            LocalKeyConsts.SCENE_OPER_TYPE_TRANSFER_PLAN_MODIFY)) {
            return;
        }
        List<KiteAttr> developerAttrs = querySceneInstService.listCommonAttrsByModuleId(context.getSceneInstId(),
            LocalKeyConsts.MODULE_ID_OPERATOR);
        if (KiteListUtils.isEmpty(developerAttrs)) {
            // ErrorConsts.BUSI_COMMON_ERROR.throwOut("未配置【经办人】属性");
        }

        String serviceOfferId = kiteSceneInst.getServiceOfferId();

        KitePosOperatorDTO operator = new KitePosOperatorDTO();
        operator.setName(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_NAME, "未配置【经办人名称】属性"));
        operator.setAddress(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_ADDRESS, "未配置【经办人地址】属性"));
        operator.setCustSatisfaction(
            this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_SATISFACTION, "未配置【经办人客户满意度】属性"));
        operator.setEmail(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_EMAIL, "未配置【经办人邮箱】属性"));
        operator.setFax(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_FAXNUM, "未配置【经办人传真号码】属性"));
        operator
            .setFixedPhone(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_FIXEDPHONE, "未配置【经办人固定电话】属性"));
        operator.setIdNo(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_CARDNUM, "未配置【经办人证件号码】属性"));
        operator.setIdType(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_CARDTYPE, "未配置【经办人证件类型】属性"));
        operator.setPostcode(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_POSTCODE, "未配置【经办人邮编】属性"));
        operator.setRemark(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_COMMENTS, "未配置【经办人备注】属性"));
        operator.setSex(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_SEX, "未配置【经办人性别】属性"));
        operator.setPhone(this.getAttrValue(developerAttrs, LocalKeyConsts.HANDLER_INFO_TEL, "未配置【经办人联系电话】属性"));

        if ("1003".equals(serviceOfferId) || "1004".equals(serviceOfferId)) {
            // 用户停机/用户复机业务经办人附件处理
            List<KiteAttr> kiteAttrList = new ArrayList<KiteAttr>();
            Iterator<KiteAttr> items = developerAttrs.iterator();
            while (items.hasNext()) {
                KiteAttr attr = items.next();
                if (StringUtils.equals(attr.getFieldName(), LocalKeyConsts.HANDLER_INFO_ATTACHMENTS)) {
                    if (!StringUtils.isEmpty(attr.getValue())) {
                        kiteAttrList.add(attr);
                        break;
                    }
                }
            }
            List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(kiteAttrList);
            if (!ListUtil.isEmpty(instAttrDTOList)) {
                List<KitePosAttachmentDTO> attachmentDtos = new ArrayList<>();
                KitePosAttachmentDTO attache = new KitePosAttachmentDTO();
                attache.setCatagory("03");
                attache.setName(instAttrDTOList.get(0).getValueDesc());
                attache.setUrl(instAttrDTOList.get(0).getValue());

                attachmentDtos.add(attache);
                operator.setAttachments(attachmentDtos);
            }
        }
        else {
            // TODO
            List<AttachmentVO> attachements = null;
            IQuerySceneCommonService querySceneCommonService = SpringUtil.getBean(IQuerySceneCommonService.class);
            List<InstAttrDTO> attrConfigs = querySceneCommonService
                .listCommonAttrs(context.getSceneInstId(), context.getCustDto().getCustId()).getResultObject();
            if (KiteListUtils.isNotEmpty(attrConfigs)) {
                for (InstAttrDTO instAttrDTO : attrConfigs) {
                    if (LocalKeyConsts.HANDLER_INFO_ATTACHMENTS.equals(instAttrDTO.getFieldName())
                        && Objects.nonNull(instAttrDTO.getValue())) {
                        attachements = JsonUtils.parseArray(instAttrDTO.getValue(), AttachmentVO.class);
                    }
                }
                if (!KiteListUtils.isEmpty(attachements)) {
                    List<KitePosAttachmentDTO> attachmentDtos = new ArrayList<>();
                    attachements.forEach((attachement) -> {
                        KitePosAttachmentDTO attache = new KitePosAttachmentDTO();
                        attache.setCatagory("03");
                        attache.setName(attachement.getFileName());
                        attache.setUrl(attachement.getAttachmentUrl());

                        attachmentDtos.add(attache);
                    });
                    operator.setAttachments(attachmentDtos);
                }
            }
        }

        context.getPosDto().setOperator(operator);
    }

}
